package common.base.service;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.sop.model.PeopleInfo;

import common.base.BaseVo;
import common.base.GridPage;
import common.base.dao.IBaseDao;

@SuppressWarnings("rawtypes")
@Transactional
public abstract class BaseServiceImpl<T,S extends BaseVo> implements BaseService<T,S> {
	private IBaseDao<T,S> baseDao;
	private Class<T> entityClass;
	
	@SuppressWarnings("unchecked")
	public BaseServiceImpl(){
		Type type = getClass().getGenericSuperclass();
		if (type instanceof ParameterizedType) {
			Type[] parameterizedType = ((ParameterizedType) type).getActualTypeArguments();
			this.entityClass = (Class<T>) parameterizedType[0];
		}
	}

	private void validatorId(Integer id) {
		if (id == null) {
		     
		}
	}

	@Override
	public T get(Integer id) {
		validatorId(id);
		return baseDao.getObjectByid(id);
    }
	
	@Override
	public List<T> findAll() {
		return baseDao.getObjectALL();
    }

	@Override
	public abstract T add(T entity);

	@Override
	public abstract T update(T entity);

	@Override
	public Boolean delete(Integer id) {
		validatorId(id);
		baseDao.delete(id);
		return true;
	}

    @Override
    public GridPage<T> findPagerBySearchVo(S searchVo) {
    	int count = baseDao.count(searchVo);  
	      
	    double dCount = (double)count;  
	    double dRows = (double)searchVo.getRows();  
	    int total = (int)Math.ceil(dCount / dRows); //total page   
	    int curPage = Math.min(total, searchVo.getPage()); //current page   
	    searchVo.setPage(curPage); //这是为了在dao中查询用的  
	    List<T> list = baseDao.queryForPageInfo(searchVo); 
	    return new GridPage<T>(curPage,total,list.size(),list);
    }

	/**
	 * 通过class获取slqMap的命名空间
	 * 
	 * @param className
	 * @return 例子：className : Druggy =>druggy
	 */
	protected String getClassName(String className) {
		return className.substring(0, 1).toLowerCase() + className.substring(1);
	}

	public IBaseDao<T,S> getBaseDao() {
		return baseDao;
	}

	public void setBaseDao(IBaseDao<T,S> baseDao) {
		this.baseDao = baseDao;
	}


	
}
